# -*- coding: utf-8 -*-
# @Author : HaleShi
# @Time : 2023-05-22
# @File : 构造字典与集和.py


# 请求头中构造字典
'''
第一行：(.*?):(.*)
第二行："$1": "$2",
dict_one.items() - dict_two.items()
'''

import time
import json
import json_tools

# 直接放入字典
DICT_ONE = {
    "screensize": "5.7",
    "ver": "5.4.7.500407",
    "recycles": "2",
    "fetchStatus": "true",
    "locale": "en_US",
    "userid": "8009182906",
    "sections": "flipboard%2Fcurator%252Fmagazine%252FMSpmMhZ_RQOUwHqTsYA3ig%253Am%253A7000000659",
    "jobid": "a-e0gl8lam",
    "norss": "1",
    "tuuid": "54679625-701c-4ea7-b419-6239914cfef9",
    "locale_cg": "zh_CN",
    "limit": "20",
    "variant": "china",
    "udid": "55e9355f5386053cb067530965d9df51984c1faf",
    "lang": "en",
    "device": "aphone-Google-Pixel%202%20XL-29",
    "aidmd5": "eacf52f00dfb8075375ba1371d399b13",
    "oaid": "",
    "wasAutoRefresh": "true"

}
DICT_TWO = {
    "screensize": "5.7",
    "ver": "5.4.7.500407",
    "recycles": "2",
    "fetchStatus": "true",
    "locale": "en_US",
    "userid": "8009182906",
    "sections": "flipboard%2Fcurator%252Fmagazine%252FMSpmMhZ_RQOUwHqTsYA3ig%253Am%253A7000000659",
    "jobid": "a-dek81kqs",
    "norss": "1",
    "tuuid": "54679625-701c-4ea7-b419-6239914cfef9",
    "locale_cg": "zh_CN",
    "limit": "20",
    "variant": "china",
    "udid": "55e9355f5386053cb067530965d9df51984c1faf",
    "lang": "en",
    "device": "aphone-Google-Pixel%202%20XL-29",
    "aidmd5": "eacf52f00dfb8075375ba1371d399b13",
    "oaid": "",
    "wasAutoRefresh": "true"
}

# 获取json文件转化成字典
FILE_ONE = r"Files/RedBoardNews1.json"
FIlE_TWO = r"Files/RedBoardNews3.json"


# 打开文件
def open_files(file_one, file_two):
    files = [file_one, file_two]
    data_list = []
    for file in files:
        with open(file, mode='r', encoding='utf-8') as f:
            data = json.load(f)
            data_list.append(data)
    return data_list[0], data_list[1]


# 比较两个json文件的不同点，打印出来并写入日志中
def json_different(json_old, json_new):
    json_diff = json_tools.diff(json_old, json_new)  # 如果完全相同，返回[]
    if not json_diff:  # 等价于 json_diff == []
        print("******两个json文件完全相同******")
    else:
        print("前后两个json文件共有【{}】处不同,以下是详细信息:".format(len(json_diff)))
        times = time.strftime("%Y%m%d-%H-%M-%S")
        content_path_name = "./logs/json_different/{}_json_compare.txt".format(times)

        content_list = []
        for i in range(len(json_diff)):
            content_list.append(f"第{i + 1}个不同点为:" + str(json_diff[i]))
            print(f"第{i + 1}个不同点为:", json_diff[i])
        content = "\n".join(content_list)
        # 写入日志中，保存成json查看
        with open(content_path_name, 'w', encoding="utf-8") as f:
            f.write(content)


# 求两个字典的交集
def different_set(dict_1, dict_2):
    d_set = dict_1.items() - dict_2.items()
    print(d_set)
    return d_set


# 字典转换成列表
def dict_to_list(dict_demo):
    list_demo = []
    list_array = []
    for key in dict_demo:
        # 返回列表的方式
        list_demo.append([key, dict_demo[key]])
        # 返回数组的方式
        list_array.append((key, dict_demo[key]))

    # 数组转换成集合
    set_demo = set(list_array)

    print("——————————————————— 这是【列-表】形式数据 ———————————————————：\n", list_demo, end='\n\n')
    print("——————————————————— 这是【集-合】形式数据 ———————————————————：\n", set_demo)

    # 可以返回列表和集合
    # return list_demo, set_demo
    return list_demo


# 利用字典的集合操作特性，批量去掉某些键：
def dict_part(dict_demo):
    dict_part = {key: dict_demo[key] for key in dict_demo.keys() - {'User-Agent', 'Upgrade-Insecure-Requests'}}
    print(dict_part)
    return dict_part


# 运行响应的方法
def run():
    data_dict_1, data_dict_2 = open_files(FILE_ONE, FIlE_TWO)
    json_different(data_dict_1, data_dict_2)
    # dict_to_list(DICT_ONE)
    # dict_part(DICT_ONE)
    # dict_to_list(DICT_ONE)


if __name__ == '__main__':
    run()
